<template>
	<div class="">
    <!-- 订单回执 -->
    <wx-web-view
      v-if="pageType == 1"
      :src="webSrc"
      @message="saveImg"
    ></wx-web-view>
		<wx-web-view v-else :src="webSrc"></wx-web-view>
	</div>
</template>

<script>
  import Vue from 'vue'
  import Storage from '@/utils/store.js'
	// import small from '@/smallApp/small'
	export default Vue.extend({
		data() {
			return {
				webSrc:'',
        pageType: '0'
			}
    },
    created() {
      this.pageType = this.$route.query.type,

      wx.setNavigationBarTitle({
        title: this.$route.query.title
      })
      window.addEventListener('wxshow', () => {
        if (Storage.fetchLocal('webSrc')) {
          this.webSrc = Storage.fetchLocal('webSrc');
        }
      })
    },
    methods: {
      saveImg(e) {
        let url = e.detail.data[0];
        // // console.log("saveImg-urlurl", url);
        const fs = wx.getFileSystemManager();
        const filePath = `${wx.env.USER_DATA_PATH}/receipt.png`;
        url = url.replace('data:image/png;base64,', '')
        fs.writeFile({
          filePath: filePath,
          data: url,
          encoding: "base64",
          success: (res) => {
            wx.showLoading({
              title: "正在保存...",
            }),
            wx.saveImageToPhotosAlbum({
              filePath: filePath,
              success: (resp) => {
                wx.hideLoading()
                wx.showModal({
                  title: "提示",
                  showCancel: false,
                  content: "已成功保存到相册!"
                });
              },
              // fail: (resppp) => {
              //   wx.hideLoading()
              //   // console.log("saveImageToPhotosAlbum失败", resppp)
              // },
              fail: (err) => {
                  if(err.errMsg === "saveImageToPhotosAlbum:fail:auth denied" || err.errMsg === "saveImageToPhotosAlbum:fail auth deny" || err.errMsg === "saveImageToPhotosAlbum:fail authorize no response") {
                    wx.showModal({
                      title: '提示',
                      content: '需要您授权保存相册',
                      showCancel: false,
                      success: () => {
                        wx.openSetting({
                          success(settingdata) {
                            if (settingdata.authSetting['scope.writePhotosAlbum']) {
                              // console.log('获取权限成功，给出再次点击图片保存到相册的提示。')
                            }else {
                              // console.log('获取权限失败，给出不给权限就无法正常使用的提示')
                            }
                          }
                        })
                      }
                    })
                  }
                }
            })
          },
        });
      },
    }
	})
</script>

<style>
</style>
